// packageC/pages/lifeNumber/lifeNumber.js



const date = new Date()
const years = []
const months = []
const days = []
for (let i = 1900; i <= date.getFullYear(); i++) { years.push(i) }
for (let i = 1; i <= 12; i++) { months.push(i) }
for (let i = 1; i <= 31; i++) { days.push(i) }



const atest = require('../../js/atest')
const getYangData = require('../../js/js-calendar-converter')

import PinyinUtils from '../../js/pinYinUtils'

Page({

  /**
   * 页面的初始数据
   */
  data: {

    date,
    years,
    months,
    days,
    year: 2023,
    month: 10,
    day: 1,
    value: [90, 1, 1],
    show: false,
    atest: atest.list,
    selectTip: '阴历',
    birthDay: '',
    testflag: false,
    year: '',
    year1: '',
    year2: '',
    month: '',
    day: '',
    numList: [],
    num1: '',
    num2: '',
    num3: '',
    num4: '',
    num5: '',
    num6: '',
    num7: '',
    keyWordList1: ['领导', '沟通', '行动', '策划', '方向', '财富、智慧', '人缘、贵人', '责任', '机会、认同、成功'],
    keyWordList2: ['自我为中心', '犹豫不决', '冲动', '心直口快', '固执', '胆小', '缓慢', '压力', '贪心'],
    //高峰
    gaoList: [{
      num: 0,
      key: ''
    }, {
      num: 0,
      key: ''
    }, {
      num: 0,
      key: ''
    }, {
      num: 0,
      key: ''
    }],
    //低谷
    diList: [{
      num: 0,
      key: ''
    }, {
      num: 0,
      key: ''
    }, {
      num: 0,
      key: ''
    }, {
      num: 0,
      key: ''
    }],
    result: "",
    // 字母对应积分
    numList: [{
      num: 1,
      code: ['A', 'J', 'S']
    },
    {
      num: 2,
      code: ['B', 'K', 'T']
    },
    {
      num: 3,
      code: ['C', 'L', 'U']
    },
    {
      num: 4,
      code: ['D', 'M', 'V']
    },
    {
      num: 5,
      code: ['E', 'N', 'W']
    },
    {
      num: 6,
      code: ['F', 'O', 'X']
    },
    {
      num: 7,
      code: ['G', 'P', 'Y']
    },
    {
      num: 8,
      code: ['H', 'Q', 'Z']
    },
    {
      num: 9,
      code: ['I', 'R']
    },
    ],
    // 名字字符串
    nameStr: '',
    nTotal: '',
    nTotalAll: '',
    oTotal: '',
    oTotalAll: '',

  },
  change(e) {
    const val = e.detail.value;
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
    })
    console.log(this.data);
  },
  showHandle(e) {
    console.log(e);
    var text = e.currentTarget.dataset.text
    this.setData({
      selectTip: text,
      show: true
    })
  },


  // 关闭弹窗
  onClose() {
    console.log(1222);
    this.setData({
      show: false
    })
  },
  // 选择时间
  timeConfirm(e) {
    console.log(this.data);
    var d = e.detail
    var m = new Date(d)
    var year = this.data.year
    var month = this.add0(this.data.month)
    var day = this.add0(this.data.day)
    var str = year + '-' + month + '-' + day

    if (this.data.selectTip == '阴历') {
      var timeObj = this.getYang(year, month, day)
      console.log(timeObj.date);
      var yTime = str + '（' + timeObj.date.split('-')[0] + '-' + timeObj.date.split('-')[1] + '-' + timeObj.date.split('-')[2] + '）'
      this.setData({
        birthDay: yTime,
        year: timeObj.date.split('-')[0],
        month: this.add0(timeObj.date.split('-')[1]),
        day: this.add0(timeObj.date.split('-')[2]),
        show: false
      })
    } else {
      this.setData({
        birthDay: str,
        year,
        month,
        day,
        show: false
      })
    }

    console.log(this.data.birthDay);
    console.log(this.data.year);
    console.log(this.data.month);
    console.log(this.data.day);
  },

  getNum() {

    if (this.data.birthDay == '' || this.data.userName == '') {
      wx.showToast({
        title: '请将信息填写完整',
        icon: 'none'
      })
      return
    }
    var year = this.data.year + '',
      year1 = '',
      year2 = '',
      month = this.data.month,
      day = this.data.day,
      num1 = '',
      num2 = '',
      num3 = '',
      num4 = '',
      num5 = '',
      num6 = '',
      num7 = '',
      result = '';


    year1 = year.substr(0, 2)
    year2 = year.substr(2, 4)

    num4 = this.numHandle(day)
    num5 = this.numHandle(month)

    num6 = this.numHandle(year1)
    num7 = this.numHandle(year2)
    num2 = this.numHandle(num4 + num5)
    num3 = this.numHandle(num6 + num7)
    num1 = this.numHandle(num2 + num3)

    console.log(num1);
    console.log(num2 + '----' + num3);
    console.log(num4 + '=' + num5 + '----' + num6 + '=' + num7);

    var glist = this.data.gaoList,
      dList = this.data.diList,
      kList1 = this.data.keyWordList1,
      kList2 = this.data.keyWordList2,
      resList = this.data.atest;

    console.log(glist);
    console.log(dList);
    console.log(kList1);
    console.log(kList2);
    console.log(resList);

    glist[0].num = num2
    glist[1].num = this.numHandle(num3 + num4)
    glist[2].num = this.numHandle(glist[0].num + glist[1].num)
    glist[3].num = this.numHandle(num3 + num5)

    dList[0].num = Math.abs(this.numHandle(num5 - num4))
    dList[1].num = Math.abs(this.numHandle(num3 - num4))
    dList[2].num = Math.abs(this.numHandle(dList[0].num - dList[1].num))
    dList[3].num = Math.abs(this.numHandle(num3 - num5))

    console.log(glist);
    console.log(dList);

    glist.forEach(ele => {
      console.log(ele);
      ele.key = ele.num == 0 ? '无' : kList1[ele.num - 1]
    })
    dList.forEach(ele => {
      ele.key = ele.num == 0 ? '无' : kList2[ele.num - 1]
    })

    console.log(glist);
    console.log(dList);


    resList.forEach(item => {
      if (item.num == num1) {
        result = item
      }
    })
    console.log(result);

    var nameList = [],
      nNum = [],
      otherNum = [],
      nList = ['A', 'E', 'I', 'O', 'U', 'V'],
      nTotal = 0,
      oTotal = 0;

    this.data.userName.split('').forEach(ele => {
      nameList.push(this.getPinYin(ele).toUpperCase().split(''))
      nNum.push(0)
      otherNum.push(0)
    })
    console.log(nameList);

    this.data.numList.forEach(item => {
      nameList.forEach((nItem, nIndex) => {
        nItem.forEach(ele => {
          if (nList.indexOf(ele) != -1) {
            item.code.forEach(cItem => {
              nNum[nIndex] += ele == cItem ? item.num : 0
            })
          } else {
            item.code.forEach(cItem => {
              if (ele == cItem) {
                otherNum[nIndex] += ele == cItem ? item.num : 0
              }
            })
          }
        })
      })
    })
    console.log(nNum);
    console.log(otherNum);



    nNum.forEach((ele, inde) => {
      nTotal += this.numHandle(ele)
      oTotal += this.numHandle(otherNum[inde])
    })
    // nNum.forEach((ele, inde) => {
    //   nTotal += ele
    //   oTotal +=otherNum[inde]
    // })

    console.log(nTotal + '——' + this.numHandle(nTotal));
    console.log(oTotal + '——' + this.numHandle(oTotal));


    this.setData({
      testflag: true,
      year,
      year1,
      year2,
      num1,
      num2,
      num3,
      num4,
      num5,
      num6,
      num7,
      gaolist: glist,
      diList: dList,
      result,
      nTotal: nTotal,
      nTotalAll: this.numHandle(nTotal),
      oTotal: oTotal,
      oTotalAll: this.numHandle(oTotal),
    })
  },
  numHandle(num) {
    num = Number(num)
    return num < 10 ? num : (num = num.toString(), this.numHandle(num.split('')[0] * 1 + num.split('')[1] * 1))
  },

  resHandle() {
    this.setData({
      testflag: false,
      currentDate: new Date().getTime(),
      birthDay: '',
      userName: ''
    })
  },

  timeCancel() {
    this.setData({
      show: false
    })
  },
  add0(num) {
    console.log(num);
    return num > 9 ? num : '0' + num
  },

  inputHandle(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(this.data.atest);
    console.log(this.getYang('2023', '08', '15'));
  },



  // 获取拼音
  getPinYin(name) {
    console.log(name);
    return PinyinUtils.chineseToPinYin(name)
  },
  // 获取阳历
  getYang(year, month, day) {
    return getYangData.calendar.lunar2solar(year, month, day)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})